package com.beidouapp.mq.service;

import java.util.Date;
import java.util.Iterator;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.beidouapp.cache.CacheConstants;
import com.beidouapp.cache.impl.RedisDefaultAppCacheHelper;
import com.wisesoft.mq.EventProcesser;

/**
 * 系统管理同步消息
 */
public class SysBaseDataTopicProcesser implements EventProcesser {
	
	private static final Logger logger = LoggerFactory.getLogger(SysBaseDataTopicProcesser.class);
	
	@Autowired
	private RedisDefaultAppCacheHelper redisCache;
	
	public enum BusinessType {
		SYNC_USER("SYNC_USER"), // 员工同步
		SYNC_ORG("SYNC_ORG"), // 机构同步
		SYNC_DEPT("SYNC_DEPT"), // 部门同步
		SYNC_ROLE("SYNC_ROLE"), // 角色同步
		SYNC_ROLE_TYPE("SYNC_ROLE_TYPE"), // 角色分类同步
		SYNC_USRE_ROLE("SYNC_USRE_ROLE"), // 用户角色同步
		SYNC_USRE_GROUP("SYNC_USRE_GROUP"), // 用户组同步
		SYNC_USRE_GROUP_MEMBER("SYNC_USRE_GROUP_MEMBER") ,// 用户组成员同步
		
		//以下业务类型不需要变更通讯录
		SYNC_RES_AUTH_MENU("SYNC_RES_AUTH_MENU"), // 角色菜单权限同步
		SYNC_RES_AUTH_UC("SYNC_RES_AUTH_UC"), // 角色用例权限同步
		//SYNC_USECASE("SYNC_USECASE"), // 用例同步
		SYNC_RES_AUTH_SYS("SYNC_RES_AUTH_SYS"); // 子系统同步
		//SYNC_PARAM("SYNC_PARAM"), // 参数同步
		//SYNC_CODE("SYNC_CODE"); // 代码同步
		String typeCode;

		BusinessType(String typeCode) {
			this.typeCode = typeCode;
		}

		public String getTypeCode() {
			return typeCode;
		}

		public void setTypeCode(String typeCode) {
			this.typeCode = typeCode;
		}
		
		public static BusinessType getBusinessTypeByCode(String typeCode){
			if(StringUtils.isBlank(typeCode)) return null;
			for(BusinessType businesstype : BusinessType.values()){
				if(businesstype.getTypeCode().equals(typeCode)){
					return businesstype;
				}
			}
			return null;
		}
	}

	public enum SyncType {
		ADD("ADD"), EDIT("EDIT"), DEL("DEL");
		String typeCode;

		SyncType(String typeCode) {
			this.typeCode = typeCode;
		}

		public String getTypeCode() {
			return typeCode;
		}

		public void setTypeCode(String typeCode) {
			this.typeCode = typeCode;
		}
		
		public static SyncType getSyncTypeByCode(String typeCode){
			if(StringUtils.isBlank(typeCode)) return null;
			for(SyncType syncType : SyncType.values()){
				if(syncType.getTypeCode().equals(typeCode)){
					return syncType;
				}
			}
			return null;
		}
	}

	@Override
	public void process(Object e) {
		/*try {
			System.out.println("收到MQ消息:" + new Date().getTime());
			System.out.println("内容:" + e.toString());
			if(e.toString().trim().startsWith("[")){
				JSONArray jsonArray =  JSONArray.fromObject(e.toString());
				@SuppressWarnings("unchecked")
				Iterator<JSONObject> iterator = (Iterator<JSONObject>)jsonArray.iterator();
				Iterator<JSONObject> it =  iterator;
				while(it.hasNext()){
					JSONObject jo = it.next();
					process(jo);
				}
			}else{
				JSONObject jo = JSONObject.fromObject(e.toString());
				process(jo);			
			}
			redisCache.removeCache(CacheConstants.CONTACTS_NAMESPACE, "departmentTreeMap");		
			redisCache.removeCache(CacheConstants.CONTACTS_NAMESPACE, "departmentTreeMap_SSR");
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error("同步联系人失败，原因："+ex.getMessage());
		}*/
	}
	
	/*private  void process(JSONObject jo) throws Exception{
		String busizId = jo.getString("busizId");
		String busizType = jo.getString("busizType");
		BusinessType businessType =	BusinessType.getBusinessTypeByCode(busizType);
		String syncTypeStr = jo.getString("syncType");
		SyncType syncType =	SyncType.getSyncTypeByCode(syncTypeStr);  
		if(businessType!=null){
			switch(businessType){
			  case SYNC_USER:
				  contactsService.syncUser2Contacts(busizId,syncType.name());
				  break;
			  case SYNC_ROLE:
			  case SYNC_ROLE_TYPE:
				  contactsService.syncRole2Contacts(busizId,syncType.name());
				  break; 
			  case SYNC_USRE_ROLE:
				  contactsService.syncRoleAndUserRelation2Contacts(busizId,syncType.name());
				  break; 
			  case SYNC_ORG:
				  contactsService.syncOrg2Contacts(busizId,  syncType.name());
				  break; 
			  case SYNC_DEPT:
				  contactsService.syncDept2Contacts(busizId, syncType.name());
				  break; 
			  case SYNC_RES_AUTH_SYS:
				  contactsService.syncResources(busizId, syncType.name());
				  break; 
			default:
				break;
			}
		}
	}*/
}
